﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace MND.Language
{
    class LanguageManager
    {
        public static LanguageManager Instance
        {
            get
            {
                if (instance == null)
                    instance = new LanguageManager();
                return instance;
            }
        }
        private static LanguageManager instance;

        private Dictionary<string, string> language = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
        public string GetString(string code)
        {
            if (language.ContainsKey(code))
                return language[code];
            else throw new LabelNotDefinedException(code);
        }

        public LanguageManager()
        {
            Load("pl");
        }

        private void Load(string lang)
        {
            DirectoryInfo dir = new DirectoryInfo(@"./Language/" + lang);
            if (!dir.Exists)
                throw new DirectoryNotFoundException(dir.FullName);
            var files = dir.GetFiles();

            var labelFile = (from file in files
                             where file.Name == "labels.lang"
                             select file).First();
            var labels = File.ReadAllLines(labelFile.FullName);
            foreach (string label in labels)
            {
                int scPos = label.IndexOf(':');
                if (scPos == -1)
                    throw new ArgumentException("Given lang (" + lang + ") has malformed labels file!");

                language.Add(label.Substring(0, scPos), label.Substring(scPos + 1));
            }
        }

        public string GetCurrentLangCode()
        {
            return "en";
        }
    }
}
